package com.tien.dao.mock;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.tien.model.Asset;

/**
 * @author Tien E-mail:g.tianxin@gmail.com
 * @version Time��2011-6-3 ����12:41:46
 */
public class LemonDatabaseMock {
	private static final Log logger = LogFactory
			.getLog(LemonDatabaseMock.class);
	private static LemonDatabaseMock instance = null;

	private HashMap<String, SecurityDatabase> securityDatabasesByAccountId = new HashMap<String, SecurityDatabase>();

	private class SecurityDatabase {
		private HashMap<String, Asset> assetsBySymbol = new HashMap<String, Asset>();

	}

	private LemonDatabaseMock() {
		loadXML();
	}

	public synchronized static LemonDatabaseMock instance() {
		if (instance == null) {
			instance = new LemonDatabaseMock();
		}

		return instance;
	}

	@SuppressWarnings("unchecked")
	private void loadXML() {
		SAXReader reader = new SAXReader();
		try {
			Document doc = (Document) reader
					.read(getClass().getClassLoader().getResourceAsStream(
							"com/tien/dao/mock/lemon-mock-db.xml"));
			List<Element> account = doc.selectNodes("/lemon/account");
			for (Element element : account) {
				loadAccount(element);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private SecurityDatabase getSecurityDatabase(String accountId) {
		SecurityDatabase db = securityDatabasesByAccountId.get(accountId);
		if (db == null) {
			db = new SecurityDatabase();
			securityDatabasesByAccountId.put(accountId, db);
		}
		return db;
	}

	private void loadAccount(Element e) {
		String accountId = e.attributeValue("id");
		Element assets = e.element("Assets");
		loadAssets(accountId, assets);
	}

	@SuppressWarnings("unchecked")
	private void loadAssets(String accountId, Element e) {
		SecurityDatabase db = getSecurityDatabase(accountId);

		List<Element> assets = e.elements("asset");
		for (Element o : assets) {
			String symbol = o.attributeValue("symbol");
			String desc = o.attributeValue("desc");
			int unrlzdQty = Integer.parseInt(o.attributeValue("unrlzdQty"));
			int rlzdQty = Integer.parseInt(o.attributeValue("rlzdQty"));
			float curPrice = Float.parseFloat(o.attributeValue("curPrice"));

			Asset asset = new Asset(symbol, desc, unrlzdQty, rlzdQty, curPrice,
					 new Date());

			db.assetsBySymbol.put(symbol, asset);
		}
		logger.info("DB : " + db.toString());
	}

	// get assets
	public List<Asset> getAllAssets(String accountId) {
		SecurityDatabase db = securityDatabasesByAccountId.get(accountId);
		if (db != null)
			return new ArrayList<Asset>(db.assetsBySymbol.values());
		return new ArrayList<Asset>(0);
	}

	public List<Asset> getAllAssets(String accountId, String symbol) {
		SecurityDatabase db = securityDatabasesByAccountId.get(accountId);
		if (db != null) {
			Asset asset = db.assetsBySymbol.get(symbol);
			if (asset != null)
				return Collections.singletonList(asset);
		}
		return new ArrayList<Asset>(0);
	}

	/**
	 * 
	 * @param accountId
	 * @param start
	 * @param limit
	 * @return List<Asset> get assets for paging
	 */

	public List<Asset> getAssets(String accountId, int start, int limit) {
		SecurityDatabase db = securityDatabasesByAccountId.get(accountId);
		List<Asset> list_paging = new ArrayList<Asset>();
		if (db != null) {
			List<Asset> list = new ArrayList<Asset>(db.assetsBySymbol.values());
			for (int i = start; i < (start + limit > list.size() ? list.size()
					: start + limit); i++) {
				list_paging.add(list.get(i));
			}
			return list_paging;
		}
		return list_paging;
	}

	public int getAssetsCount(String accountId) {
		SecurityDatabase db = securityDatabasesByAccountId.get(accountId);
		if (db != null)
			return new ArrayList<Asset>(db.assetsBySymbol.values()).size();
		return 0;
	}

	public static void main(String[] args) {
		List<Asset> list = LemonDatabaseMock.instance().getAllAssets("xt40919");
		System.out.println(list.size());
	}

}
